package com.academysmart.bookagolf.provider;

import com.academysmart.bookagolf.config.ApplicationConfigValues;
import com.academysmart.bookagolf.encryption.GolfApi;
import com.academysmart.bookagolf.logging.Loggers;
import com.academysmart.bookagolf.model.Statistic;

import java.util.Date;

/**
 * Created by Andrew on 02.03.14.
 */
public class StatisticProvider {
    private static Statistic statistic = null;
    private static Date dateFrom;
    private static Date dateTo;

    private StatisticProvider() {
    }

    public static Statistic getStatistic(Date dateFrom, Date dateTo) {
        if (statistic == null || (!StatisticProvider.dateFrom.equals(dateFrom) || !StatisticProvider.dateTo.equals(dateTo))) {
            try {
                // GolfApi golfApi = new GolfApi();
                StatisticProvider.dateFrom = dateFrom;
                StatisticProvider.dateTo = dateTo;
                GolfApi golfApi = GolfApiProvider.getGolfApi();
                statistic = golfApi.getStatistic(ApplicationConfigValues.CLUB_ID, dateFrom, dateTo);
                statistic.setStatisticsEquipments(golfApi.getStatisticEquipment(ApplicationConfigValues.CLUB_ID, dateFrom, dateTo));
                statistic.setHolesList(golfApi.getStatisticHoles(ApplicationConfigValues.CLUB_ID, dateFrom, dateTo));
            } catch (Exception e) {
                // e.printStackTrace();
                Loggers.FILE_LOGGER.error("Error occured.", e);
            }
        }
        return statistic;
    }


}
